//题目
if (window.$) {
	if (!window.zyk) {
		window.zyk = {
			uploadSubject : function() {
				debugger;
				return JSON
						.stringify({
							'灌注桩后注浆技术采用循环注浆时，注浆分（  ）循环。' : [ '一次','三次' ]
						});
			},
			show : function(m) {
				console.log(m);
			},
			reload : function() {
				console.log('reload');
				location.reload();
			}
		};
	}

	// 处理试题
	// 第一遍搜索，把所有的题目搜索出来
	if ($('.kaishikaoshi').length) {
		if ($('.kaishikaoshi').attr('answer') == 'yes') {
		} else {
			// 先将答题锁定
			$('.kaishikaoshi').attr('answer', 'yes');
			var subjectAll = [];
			// 单选题
			$('ul[data-type="1"]').each(
					function(i, e) {
						var dx = $(e);
						var subject = {
							answers : [],
							content : '',
							analysis : ''
						};
						subjectAll.push(subject);

						subject.content = $.trim(dx.find('li:eq(0)').text())
								.replace(/^\d+、/, '');
						subject.analysis = $.trim(dx.find('.ansTrue:last')
								.text().replace('试题解析:', ''));
						dx.find(".xti").each(
								function(i1, e1) {
									var answer = $.trim(
											$(e1).find('input').attr('title'))
											.substring(2);
									subject.answers.push(answer);
								});
					});

			// 多选题
			$('ul[data-type="2"]').each(
					function(i, e) {
						var dx = $(e);
						var subject = {
							answers : [],
							content : '',
							analysis : ''
						};
						subjectAll.push(subject);

						subject.content = $.trim(dx.find('li:eq(0)').text())
								.replace(/^\d+、/, '');
						subject.analysis = $.trim(dx.find('.ansTrue:last')
								.text().replace('试题解析:', ''));
						dx.find(".xti").each(
								function(i1, e1) {
									var answer = $.trim(
											$(e1).find('input').attr('title'))
											.substring(2);
									subject.answers.push(answer);
								});
					});

			// 对错题
			$('ul[data-type="3"]').each(
					function(i, e) {
						var dx = $(e);
						var subject = {
							answers : [],
							content : '',
							analysis : ''
						};
						subjectAll.push(subject);

						subject.content = $.trim(dx.find('li:eq(0)').text())
								.replace(/^\d+、/, '');
						subject.analysis = $.trim(dx.find('.ansTrue:last')
								.text().replace('试题解析:', ''));
						dx.find(".xti").each(
								function(i1, e1) {
									var answer = $.trim($(e1).find('input')
											.attr('title'));
									subject.answers.push(answer);
								});
					});

			// 正确答案
			var rights = JSON.parse(window.zyk.uploadSubject(JSON
					.stringify(subjectAll)));
			debugger;
			if (rights) {
				var rightNum = 0;
				$('ul[data-type="1"],ul[data-type="2"],ul[data-type="3"]')
						.each(
								function(i, e) {
									var dx = $(e);
									var dataType = dx.attr('data-type');
									// 题目
									var content = $.trim(
											dx.find('li:eq(0)').text())
											.replace(/^\d+、/, '');
									// 正确答案
									var rightAnswers = rights[content];
									if (!rightAnswers) {
										dx
												.find('li:eq(0)')
												.after(
														'<p style="color:red;">没有正确答案</p>');
									} else {
										var rstring = '';

										$(rightAnswers).each(
												function(i1, e1) {
													rstring = rstring
															+ (i1 + 1) + "。"
															+ e1 + '<br>';
												});
										dx.find('li:eq(0)').after(
												'<p style="color:red;">正确答案：'
														+ rstring + '</p>');
										rightNum++;
									}
									dx
											.find(".xti")
											.each(
													function(i1, e1) {
														var answer;

														if (dataType != '3') {
															answer = $
																	.trim(
																			$(
																					e1)
																					.find(
																							'input')
																					.attr(
																							'title'))
																	.substring(
																			2);
														} else {
															// 单选题
															answer = $
																	.trim($(e1)
																			.find(
																					'input')
																			.attr(
																					'title'))
														}

														if (rightAnswers) {
															// 根据正确答案选择
															for (var i = 0; i < rightAnswers.length; i++) {
																var ra = rightAnswers[i];

																if (ra == answer) {
																	$(e1)
																			.find(
																					'.layui-form-radio')
																			.click();
																	$(e1)
																			.find(
																					'.layui-form-checkbox')
																			.click();
																} else {
																	// 该选项与当前答案不匹配
																}
															}
														} else {
															// 随机决定要不要选择，为了确保至少有一项会被选中，则答案1必须选取
															if (i1 == 0) {
																$(e1)
																		.find(
																				'.layui-form-radio')
																		.click();
																$(e1)
																		.find(
																				'.layui-form-checkbox')
																		.click();
															} else {
																var rint = parseInt(Math
																		.random() * 3);

																if (rint == 0) {
																	// 选择
																	$(e1)
																			.find(
																					'.layui-form-radio')
																			.click();
																	$(e1)
																			.find(
																					'.layui-form-checkbox')
																			.click();
																} else {
																	// 该选项不自动选择
																}
															}
														}
													});
								});

				alert('获取到的正确答案数：' + rightNum);

				if (rightNum >= 40) {
					var submitTime = 1000 * 60 * (Math
							.floor(Math.random() * 1 + 2) + 5);

					var start = new Date();
					var setInt = setInterval(
							function() {
								var now = new Date();
								if (now.getTime() - start.getTime() > submitTime) {
									// 提交时间不可过早
									alert('提交答案');
									$('#submit').click();
									clearInterval(setInt);
								} else {
									var s = parseInt((submitTime - (now
											.getTime() - start.getTime())) / 1000);

									if (s > 60) {
										s = parseInt(s / 60);

										alert(s + '分后提交答案');
									} else {
										alert(s + '秒后提交答案');
									}
								}
							}, 10000);
				} else {
					debugger;
					alert('返回正确答案的题目小于只有' + rightNum + '个，刷新重试！');
					window.location.reload();
				}
			} else {
				alert('题目上传失败，刷新后重试！');
				window.location.reload();
			}
		}
	}
}